<!-- Controller -->
<?php
error_reporting(-1);
ini_set('display_errors', 1);

require APPPATH . 'core/feed/feed_bank.php';

class video extends CI_Controller {

    protected $data;

    function __construct() {
        $this->_initdata();
    }

    function index() {
        $this->data['page_title'] = "Video";
        $this->data['page_css'] = "video";
        $this->data['keyword'] = "Nhập nội dung cần tìm";
        $this->data['page_name'] = "video";
        
        //get list video
        $this->data['aVideo'] = $this->showVideoList();
        
        $this->show($this->data);
    }
    
    function _initdata() {
        parent::__construct();
        session_start();
        $this->load->helper('eximcached');
        
        $this->load->model('Frontendmodel');
        $this->load->model('Fe_adv_model');
        $this->load->model('Fe_company_model');
        $this->load->model('Fe_docs_model');
        $this->load->model('Fe_forwarding_model');
        $this->load->model('Fe_knowledge_model');
        $this->load->model('Fe_faq_model');
        
        //load submenu and footer
        list($this->data['aLatestNews']) = get_latest_news($this->Frontendmodel, 7);
        list($this->data['aCate2']) = get_newscate_list(NEWS_CATE_VBPQ, $this->Frontendmodel);
        $this->data['aCate3'] = $this->Fe_knowledge_model->get_all_cate();
        $this->data['aCate4'] = _get_all_cate_customs($this->Frontendmodel);
        $this->data['aCate5'] = $this->Fe_forwarding_model->get_all_cate();
        $this->data['aCate6'] = $this->Fe_faq_model->_get_faq_cate_list();

        $this->data['page_header'] = "header";
        $this->data['page_rightbar'] = "rightbar";
        $this->data['page_footer'] = "footer";
        $this->data['base_url'] = $this->config->item('base_url');
        $this->data['img_url'] = $this->config->item('img_url');
        $this->data['current_url'] = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        $this->data['aCityList'] = get_cities_list($this->Frontendmodel);
        $feed = new feed_bank();
        $this->data['gold'] = get_feed_gold($feed);
        $this->data['exchange_rate'] = get_feed_exchange($feed);

    }
    
    function paging($count, $curPage, $pageSize){
        
        $numberPage = ceil($count / $pageSize);
        
        $startPage = 1;
        $endPage = $numberPage;
        $prev = $curPage > 1 ? $curPage - 1 : $curPage;
        $next = $curPage < $numberPage ? $curPage + 1 : $numberPage;
        
        if($endPage > 5){
            $startPage = $curPage - 2 < 1 ? 1 : $curPage - 2;
            $endPage = $curPage + 2 > $numberPage ? $numberPage : $curPage + 2;
        }
        
        if(isset($_GET["txtSearch"])){
            $html = '<a href="http://eximgate.vn/video/tim-kiem/' . $prev . '?txtSearch=' . $_GET["txtSearch"] . '" class="paging-previous_next"><img src="http://eximgate.vn/style/images/img20.gif" border="0"></a>';
            for($i=$startPage; $i <= $endPage; $i++){
                if($curPage == $i){
                    $html .= '<a href="http://eximgate.vn/video/tim-kiem/' . $i . '?txtSearch=' . $_GET["txtSearch"] . '" class="paging-item-selected">' . $i . '</a>';
                }
                else{
                    $html .= '<a href="http://eximgate.vn/video/tim-kiem/' . $i . '?txtSearch=' . $_GET["txtSearch"] . '" class="paging-item">' . $i . '</a>';
                }
            }
            $html .= '<a href="http://eximgate.vn/video/tim-kiem/' . $next . '?txtSearch=' . $_GET["txtSearch"] . '" class="paging-previous_next"><img src="http://eximgate.vn/style/images/img21.gif" border="0"></a>';
        }
        else{
            $html = '<a href="http://eximgate.vn/video/page/' . $prev . '" class="paging-previous_next"><img src="http://eximgate.vn/style/images/img20.gif" border="0"></a>';
            for($i=$startPage; $i <= $endPage; $i++){
                if($curPage == $i){
                    $html .= '<a href="http://eximgate.vn/video/page/' . $i . '" class="paging-item-selected">' . $i . '</a>';
                }
                else{
                    $html .= '<a href="http://eximgate.vn/video/page/' . $i . '" class="paging-item">' . $i . '</a>';
                }
            }
            $html .= '<a href="http://eximgate.vn/video/page/' . $next . '" class="paging-previous_next"><img src="http://eximgate.vn/style/images/img21.gif" border="0"></a>';
        }
                                
        return $html;
    }
    
    function paging_mobile($count, $curPage, $pageSize){
        
        $numberPage = ceil($count / $pageSize);
        
        $startPage = 1;
        $endPage = $numberPage;
        $prev = $curPage > 1 ? $curPage - 1 : $curPage;
        $next = $curPage < $numberPage ? $curPage + 1 : $numberPage;
        
        if($endPage > 5){
            $startPage = $curPage - 2 < 1 ? 1 : $curPage - 2;
            $endPage = $curPage + 2 > $numberPage ? $numberPage : $curPage + 2;
        }
        
        $html = '<div class="center"><nav><ul class="pagination">';
        
        if(isset($_GET["txtSearch"])){
            $html .= '<li class="disabled"><a href="'.$this->config->item('base_url').'video/tim-kiem/' . $prev . '?txtSearch=' . $_GET["txtSearch"] . '&txtCustomsCate=' . $_GET["txtCustomsCate"] . '">&laquo;</a></li>';
            for($i=$startPage; $i <= $endPage; $i++){
                if($curPage == $i){
                    $html .= '<li class="active"><a href="'.$this->config->item('base_url').'video/tim-kiem/' . $i . '?txtSearch=' . $_GET["txtSearch"] . '&txtCustomsCate=' . $_GET["txtCustomsCate"] . '">' . $i . '<span class="sr-only">(current)</span></a></li>';
                }
                else{
                    $html .= '<li class="disabled"><a href="'.$this->config->item('base_url').'video/tim-kiem/' . $i . '?txtSearch=' . $_GET["txtSearch"] . '&txtCustomsCate=' . $_GET["txtCustomsCate"] . '">' . $i . '</a></li>';
                }
            }
            $html .= '<li class="disabled"><a href="'.$this->config->item('base_url').'video/tim-kiem/' . $next . '?txtSearch=' . $_GET["txtSearch"] . '&txtCustomsCate=' . $_GET["txtCustomsCate"] . '">&raquo;</a></li>';
        }
        else{
            $html .= '<li class="disabled"><a href="'.$this->config->item('base_url').'video/page/' . $prev . '">&laquo;</a></li>';
            for($i=$startPage; $i <= $endPage; $i++){
                if($curPage == $i){
                    $html .= '<li class="active"><a href="'.$this->config->item('base_url').'video/page/' . $i . '">' . $i . '<span class="sr-only">(current)</span></a></li>';
                }
                else{
                    $html .= '<li class="disabled"><a href="'.$this->config->item('base_url').'video/page/' . $i . '">' . $i . '</a></li>';
                }
            }
            $html .= '<li class="disabled"><a href="'.$this->config->item('base_url').'video/page/' . $next . '">&raquo;</a></li>';
        }
                
        $html .= '</ul></nav></div>';
        
        return $html;
    }
    
    function showVideoList(){
        
        $titleSearch = "";
        if(isset($_GET["txtSearch"])){
            $titleSearch = $_GET["txtSearch"];
        }
        
        $curPage = $this->uri->segment(3);
        if($curPage == ""){
            $curPage = 1;
        }
        
        $pageSize = 20;
        list($count, $result) = _get_list_video($this->Frontendmodel, ($curPage-1)*$pageSize, $pageSize, $titleSearch);
               
        $this->load->library('Mobile_Detect');
        $detect = new Mobile_Detect();
        if ($detect->isMobile() || $detect->isTablet() || $detect->isAndroidOS())
            $this->data['paging'] = $this->paging_mobile($count, $curPage, $pageSize);
        else
            $this->data['paging'] = $this->paging($count, $curPage, $pageSize);
        
        return $result;
    }
}